package bpm.dao.impl.process;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import bpm.domain.process.ProcessDefinition;
import bpm.domain.process.ProcessInstance;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class ProcessInstanceMapper implements RowMapper {
	private boolean alive;
	
	public ProcessInstanceMapper() {}
	public ProcessInstanceMapper(boolean alive) {
		this.alive = alive;
	}

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		ProcessInstance pi = new ProcessInstance();
		
		pi.setId(rs.getInt("id"));
		pi.setName(rs.getString("casename"));
		pi.setDocumentId(rs.getString("casedata1"));
		pi.setStarter(rs.getString("startuser"));
		pi.setLastPerformer(rs.getString("lastperformer"));
		pi.setState(ProcessInstance.State.idOf(rs.getInt("state")));
		if(rs.getInt("subprocess") == 0) {
			pi.setSubProcess(false);
		} else {
			pi.setSubProcess(true);
		}
		
		String pdid = rs.getString("processid");
		if(pdid != null) {
			ProcessDefinition pd = new ProcessDefinition(pdid);
			pd.setName(rs.getString("processname"));
			pi.setProcessDefinition(pd);
		}
		
		pi.setStartTime(new Date(rs.getLong("startdate")));
		pi.setLastUpdateTime(new Date(rs.getLong("updatedate")));
		
		pi.setAlive(this.alive);
		
		return pi;
	}

}
